set more off

/*************************************************************************************************************/
/*************************************************************************************************************/
/*This program creates Appendix figures 3, 4, and 7 using identical code but with different sets of tornadoes*/
/*************************************************************************************************************/
/*************************************************************************************************************/

/***************************************************/
/*Appendix Figure 3 - For No-Aid Wayne, NE Tornado */
/***************************************************/

cd "C:\Research\Tornadoes\0515Miles_CountyM\"

//local damagelevel "alltreat g1treat g2treat g3treat" 
local damagelevel "g3treat" 

foreach l of local damagelevel { 
use "Temp\temp_program_5_`l'.dta", clear

gen group = "ia_t"  if individual_assistance == 1 & treated == 1
replace group = "ia_c" if individual_assistance == 1 & treated == 0
replace group = "nia_t" if individual_assistance == 0 & treated == 1
replace group = "nia_c" if individual_assistance == 0 & treated == 0
drop if group == ""


gen event_time = "m4" if ysm4 == 1
replace event_time = "m3" if ysm3 ==1
replace event_time = "m2" if ysm2 ==1
replace event_time = "m1" if ysm1 ==1
replace event_time = "p0" if ysp0 ==1
replace event_time = "p1" if ysp1 ==1
replace event_time = "p2" if ysp2 ==1
replace event_time = "p3" if ysp3 ==1
replace event_time = "p4" if ysp4 ==1

global LHSvarsstubs "firm emp sales" 
global industrystubs "all" 

foreach k of global LHSvarsstubs { 
foreach j of global industrystubs { 

reg ln_`k'_`j' yeardum*
predict ln_`k'_`j'_yt
gen ln_`k'_`j'_rd=ln_`k'_`j'-ln_`k'_`j'_yt
}
}

/********************************/
/*Keeping only the Wayne Tornado*/
/********************************/

drop if tornado_id ~=111 & individual_assistance == 0 


collapse (mean) *_rd, by(group event_time) 
drop if event_time == ""
reshape wide *_rd, i(event_time) j(group) s


save "Temp\temp_residual_graph_comb.dta", replace

use "Temp\temp_residual_graph_comb.dta", clear

gen eyear = .
label variable eyear "Tornado Event Year"
forvalues y = 1/4 {
   replace eyear = -`y' if event_time == "m`y'"
}
forvalues y = 1/4 {
  replace eyear = `y' if event_time == "p`y'"
}
replace eyear = 0 if event_time == "p0"
tsset eyear

foreach k of global LHSvarsstubs { 
foreach j of global industrystubs { 

label var ln_`k'_`j'_rdia_c "Nearby-Aid"
label var ln_`k'_`j'_rdia_t "Hit-Aid"
label var ln_`k'_`j'_rdnia_c "Nearby-No Aid"
label var ln_`k'_`j'_rdnia_t "Hit-No Aid"
gen ln_`k'_`j'_ia_d = ln_`k'_`j'_rdia_t - ln_`k'_`j'_rdia_c
gen ln_`k'_`j'_nia_d = ln_`k'_`j'_rdnia_t - ln_`k'_`j'_rdnia_c

label var ln_`k'_`j'_ia_d "Aid"
label var ln_`k'_`j'_nia_d "No Aid"

}
}
}
save "Temp\temp_program_5_residual_graph_part0.dta", replace
use "Temp\temp_program_5_residual_graph_part0.dta", clear
/*A second method*/
/*Combine the trends graphs on the left hand side first, 
and then combine graphs on the right hand side.
Finally, combine them together.*/
 
 twoway (connected ln_firm_all_rdnia_c eyear, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) ///
    (connected ln_firm_all_rdia_c eyear, lcolor(red) msymbol(O) mcolor(red)) ///
	(connected ln_firm_all_rdnia_t eyear, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) ///
    (connected ln_firm_all_rdia_t eyear, lcolor(dkorange) msymbol(O) mcolor(dkorange)), ///
	ytitle("{bf:Log No. of Establishments}") xline(-1, lcolor(black)) xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_firm_all_residual_g3treat, replace)
	graph export "Figures\ResidualBusiness\Trends_1_1.pdf", as(pdf) replace
	
twoway (connected ln_emp_all_rdnia_c eyear, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) ///
    (connected ln_emp_all_rdia_c eyear, lcolor(red) msymbol(O) mcolor(red)) ///
	(connected ln_emp_all_rdnia_t eyear, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) ///
    (connected ln_emp_all_rdia_t eyear, lcolor(dkorange) msymbol(O) mcolor(dkorange)), ///
	ytitle("{bf:Log No. of Employees}") xline(-1, lcolor(black)) xtitle(" ") legend(size(small) symxsize(*.75)) ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_emp_all_residual_g3treat, replace)
	graph export "Figures\ResidualBusiness\Trends_2_1.pdf", as(pdf) replace
	
	grc1leg Figures\ResidualBusiness\ln_firm_all_residual_g3treat.gph  Figures\ResidualBusiness\ln_emp_all_residual_g3treat.gph , col(1) ysize(17) xsize(11) legendfrom(Figures\ResidualBusiness\ln_emp_all_residual_g3treat.gph) saving(Figures\ResidualBusiness\combined1, replace) 
	graph display, ysize(17) xsize(11) scheme(s1mono) 
    graph export "Figures\ResidualBusiness\Trends_part1.pdf", as(pdf) replace

twoway (connected ln_firm_all_nia_d eyear, lcolor(blue) msymbol(O) mcolor(blue) msymbol(triangle) lpattern(dash)) ///
    (connected ln_firm_all_ia_d eyear, lcolor(red) msymbol(O) mcolor(red)), ///
	ytitle("{bf:Difference in Log(Est)}") xline(-1, lcolor(black))  xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_firm_all_residual_DD_g3treat, replace)	
	graph export "Figures\ResidualBusiness\Trends_1_2.pdf", as(pdf) replace
	
twoway (connected ln_emp_all_nia_d eyear, lcolor(blue) msymbol(O) mcolor(blue) msymbol(triangle) lpattern(dash)) ///
    (connected ln_emp_all_ia_d eyear, lcolor(red) msymbol(O) mcolor(red)), ///
	ytitle("{bf:Difference in Log(Emp)}") xline(-1, lcolor(black))  xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat, replace)	
	graph export "Figures\ResidualBusiness\Trends_2_2.pdf", as(pdf) replace
	
	
	grc1leg Figures\ResidualBusiness\ln_firm_all_residual_DD_g3treat.gph  Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat.gph, col(1) ysize(17) xsize(11) legendfrom(Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat.gph) saving(Figures\ResidualBusiness\combined2, replace)
	graph display, ysize(17) xsize(11) scheme(s1mono) 
    graph export "Figures\ResidualBusiness\Trends_part2.pdf", as(pdf) replace

cd "C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\"	
	gr combine combined1.gph combined2.gph, ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white))
	graph display, ysize(17) xsize(20) scheme(s2mono) 
	graph export "Appendix_Figure3.pdf", as(pdf) replace
	

/***************************************************/
/*Appendix Figure 4 - For All Tornadoes            */
/***************************************************/

cd "C:\Research\Tornadoes\0515Miles_CountyM\"

//local damagelevel "alltreat g1treat g2treat g3treat" 
local damagelevel "g3treat" 

foreach l of local damagelevel { 
use "Temp\temp_program_5_`l'.dta", clear

gen group = "ia_t"  if individual_assistance == 1 & treated == 1
replace group = "ia_c" if individual_assistance == 1 & treated == 0
replace group = "nia_t" if individual_assistance == 0 & treated == 1
replace group = "nia_c" if individual_assistance == 0 & treated == 0
drop if group == ""


gen event_time = "m4" if ysm4 == 1
replace event_time = "m3" if ysm3 ==1
replace event_time = "m2" if ysm2 ==1
replace event_time = "m1" if ysm1 ==1
replace event_time = "p0" if ysp0 ==1
replace event_time = "p1" if ysp1 ==1
replace event_time = "p2" if ysp2 ==1
replace event_time = "p3" if ysp3 ==1
replace event_time = "p4" if ysp4 ==1

global LHSvarsstubs "firm emp sales" 
global industrystubs "all" 

foreach k of global LHSvarsstubs { 
foreach j of global industrystubs { 

reg ln_`k'_`j' yeardum*
predict ln_`k'_`j'_yt
gen ln_`k'_`j'_rd=ln_`k'_`j'-ln_`k'_`j'_yt
}
}

drop *_yt

collapse (mean) *_rd, by(group event_time) 
drop if event_time == ""
reshape wide *_rd, i(event_time) j(group) s


save "Temp\temp_residual_graph_comb.dta", replace

use "Temp\temp_residual_graph_comb.dta", clear

gen eyear = .
label variable eyear "Tornado Event Year"
forvalues y = 1/4 {
   replace eyear = -`y' if event_time == "m`y'"
}
forvalues y = 1/4 {
  replace eyear = `y' if event_time == "p`y'"
}
replace eyear = 0 if event_time == "p0"
tsset eyear

foreach k of global LHSvarsstubs { 
foreach j of global industrystubs { 

label var ln_`k'_`j'_rdia_c "Nearby-Aid"
label var ln_`k'_`j'_rdia_t "Hit-Aid"
label var ln_`k'_`j'_rdnia_c "Nearby-No Aid"
label var ln_`k'_`j'_rdnia_t "Hit-No Aid"
gen ln_`k'_`j'_ia_d = ln_`k'_`j'_rdia_t - ln_`k'_`j'_rdia_c
gen ln_`k'_`j'_nia_d = ln_`k'_`j'_rdnia_t - ln_`k'_`j'_rdnia_c

label var ln_`k'_`j'_ia_d "Aid"
label var ln_`k'_`j'_nia_d "No Aid"

}
}
}
save "Temp\temp_program_5_residual_graph_part0.dta", replace
use "Temp\temp_program_5_residual_graph_part0.dta", clear
/*A second method*/
/*Combine the trends graphs on the left hand side first, 
and then combine graphs on the right hand side.
Finally, combine them together.*/
 
 twoway (connected ln_firm_all_rdnia_c eyear, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) ///
    (connected ln_firm_all_rdia_c eyear, lcolor(red) msymbol(O) mcolor(red)) ///
	(connected ln_firm_all_rdnia_t eyear, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) ///
    (connected ln_firm_all_rdia_t eyear, lcolor(dkorange) msymbol(O) mcolor(dkorange)), ///
	ytitle("{bf:Log No. of Establishments}") xline(-1, lcolor(black)) xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_firm_all_residual_g3treat, replace)
	graph export "Figures\ResidualBusiness\Trends_1_1.pdf", as(pdf) replace
	
twoway (connected ln_emp_all_rdnia_c eyear, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) ///
    (connected ln_emp_all_rdia_c eyear, lcolor(red) msymbol(O) mcolor(red)) ///
	(connected ln_emp_all_rdnia_t eyear, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) ///
    (connected ln_emp_all_rdia_t eyear, lcolor(dkorange) msymbol(O) mcolor(dkorange)), ///
	ytitle("{bf:Log No. of Employees}") xline(-1, lcolor(black)) xtitle(" ") legend(size(small) symxsize(*.75)) ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_emp_all_residual_g3treat, replace)
	graph export "Figures\ResidualBusiness\Trends_2_1.pdf", as(pdf) replace
	
	grc1leg Figures\ResidualBusiness\ln_firm_all_residual_g3treat.gph  Figures\ResidualBusiness\ln_emp_all_residual_g3treat.gph , col(1) ysize(17) xsize(11) legendfrom(Figures\ResidualBusiness\ln_emp_all_residual_g3treat.gph) saving(Figures\ResidualBusiness\combined1, replace) 
	graph display, ysize(17) xsize(11) scheme(s1mono) 
    graph export "Figures\ResidualBusiness\Trends_part1.pdf", as(pdf) replace

twoway (connected ln_firm_all_nia_d eyear, lcolor(blue) msymbol(O) mcolor(blue) msymbol(triangle) lpattern(dash)) ///
    (connected ln_firm_all_ia_d eyear, lcolor(red) msymbol(O) mcolor(red)), ///
	ytitle("{bf:Difference in Log(Est)}") xline(-1, lcolor(black))  xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_firm_all_residual_DD_g3treat, replace)	
	graph export "Figures\ResidualBusiness\Trends_1_2.pdf", as(pdf) replace
	
twoway (connected ln_emp_all_nia_d eyear, lcolor(blue) msymbol(O) mcolor(blue) msymbol(triangle) lpattern(dash)) ///
    (connected ln_emp_all_ia_d eyear, lcolor(red) msymbol(O) mcolor(red)), ///
	ytitle("{bf:Difference in Log(Emp)}") xline(-1, lcolor(black))  xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat, replace)	
	graph export "Figures\ResidualBusiness\Trends_2_2.pdf", as(pdf) replace
	
	
	grc1leg Figures\ResidualBusiness\ln_firm_all_residual_DD_g3treat.gph  Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat.gph, col(1) ysize(17) xsize(11) legendfrom(Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat.gph) saving(Figures\ResidualBusiness\combined2, replace)
	graph display, ysize(17) xsize(11) scheme(s1mono) 
    graph export "Figures\ResidualBusiness\Trends_part2.pdf", as(pdf) replace

cd "C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\"	
	gr combine combined1.gph combined2.gph, ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white))
	graph display, ysize(17) xsize(20) scheme(s2mono) 
	graph export "Appendix_Figure4.pdf", as(pdf) replace
	

/***************************************************/
/*Appendix Figure 7 - For our prefered sample      */
/***************************************************/

cd "C:\Research\Tornadoes\0515Miles_CountyM\"

//local damagelevel "alltreat g1treat g2treat g3treat" 
local damagelevel "g3treat" 

foreach l of local damagelevel { 
use "Temp\temp_program_5_`l'.dta", clear

gen group = "ia_t"  if individual_assistance == 1 & treated == 1
replace group = "ia_c" if individual_assistance == 1 & treated == 0
replace group = "nia_t" if individual_assistance == 0 & treated == 1
replace group = "nia_c" if individual_assistance == 0 & treated == 0
drop if group == ""


gen event_time = "m4" if ysm4 == 1
replace event_time = "m3" if ysm3 ==1
replace event_time = "m2" if ysm2 ==1
replace event_time = "m1" if ysm1 ==1
replace event_time = "p0" if ysp0 ==1
replace event_time = "p1" if ysp1 ==1
replace event_time = "p2" if ysp2 ==1
replace event_time = "p3" if ysp3 ==1
replace event_time = "p4" if ysp4 ==1

global LHSvarsstubs "firm emp sales" 
global industrystubs "all" 

foreach k of global LHSvarsstubs { 
foreach j of global industrystubs { 

reg ln_`k'_`j' yeardum*
predict ln_`k'_`j'_yt
gen ln_`k'_`j'_rd=ln_`k'_`j'-ln_`k'_`j'_yt
}
}

drop *_yt


/********************************/
/*Dropping the Wayne Tornado    */
/********************************/

drop if tornado_id ==111

collapse (mean) *_rd, by(group event_time) 
drop if event_time == ""
reshape wide *_rd, i(event_time) j(group) s


save "Temp\temp_residual_graph_comb.dta", replace

use "Temp\temp_residual_graph_comb.dta", clear

gen eyear = .
label variable eyear "Tornado Event Year"
forvalues y = 1/4 {
   replace eyear = -`y' if event_time == "m`y'"
}
forvalues y = 1/4 {
  replace eyear = `y' if event_time == "p`y'"
}
replace eyear = 0 if event_time == "p0"
tsset eyear

foreach k of global LHSvarsstubs { 
foreach j of global industrystubs { 

label var ln_`k'_`j'_rdia_c "Nearby-Aid"
label var ln_`k'_`j'_rdia_t "Hit-Aid"
label var ln_`k'_`j'_rdnia_c "Nearby-No Aid"
label var ln_`k'_`j'_rdnia_t "Hit-No Aid"
gen ln_`k'_`j'_ia_d = ln_`k'_`j'_rdia_t - ln_`k'_`j'_rdia_c
gen ln_`k'_`j'_nia_d = ln_`k'_`j'_rdnia_t - ln_`k'_`j'_rdnia_c

label var ln_`k'_`j'_ia_d "Aid"
label var ln_`k'_`j'_nia_d "No Aid"

}
}
}
save "Temp\temp_program_5_residual_graph_part0.dta", replace
use "Temp\temp_program_5_residual_graph_part0.dta", clear
/*A second method*/
/*Combine the trends graphs on the left hand side first, 
and then combine graphs on the right hand side.
Finally, combine them together.*/
 
 twoway (connected ln_firm_all_rdnia_c eyear, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) ///
    (connected ln_firm_all_rdia_c eyear, lcolor(red) msymbol(O) mcolor(red)) ///
	(connected ln_firm_all_rdnia_t eyear, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) ///
    (connected ln_firm_all_rdia_t eyear, lcolor(dkorange) msymbol(O) mcolor(dkorange)), ///
	ytitle("{bf:Log No. of Establishments}") xline(-1, lcolor(black)) xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_firm_all_residual_g3treat, replace)
	graph export "Figures\ResidualBusiness\Trends_1_1.pdf", as(pdf) replace
	
twoway (connected ln_emp_all_rdnia_c eyear, lcolor(blue) msymbol(triangle) mcolor(blue) lpattern(dash)) ///
    (connected ln_emp_all_rdia_c eyear, lcolor(red) msymbol(O) mcolor(red)) ///
	(connected ln_emp_all_rdnia_t eyear, lcolor(dkgreen) msymbol(triangle) mcolor(dkgreen) lpattern(dash) ) ///
    (connected ln_emp_all_rdia_t eyear, lcolor(dkorange) msymbol(O) mcolor(dkorange)), ///
	ytitle("{bf:Log No. of Employees}") xline(-1, lcolor(black)) xtitle(" ") legend(size(small) symxsize(*.75)) ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_emp_all_residual_g3treat, replace)
	graph export "Figures\ResidualBusiness\Trends_2_1.pdf", as(pdf) replace
	
	grc1leg Figures\ResidualBusiness\ln_firm_all_residual_g3treat.gph  Figures\ResidualBusiness\ln_emp_all_residual_g3treat.gph , col(1) ysize(17) xsize(11) legendfrom(Figures\ResidualBusiness\ln_emp_all_residual_g3treat.gph) saving(Figures\ResidualBusiness\combined1, replace) 
	graph display, ysize(17) xsize(11) scheme(s1mono) 
    graph export "Figures\ResidualBusiness\Trends_part1.pdf", as(pdf) replace

twoway (connected ln_firm_all_nia_d eyear, lcolor(blue) msymbol(O) mcolor(blue) msymbol(triangle) lpattern(dash)) ///
    (connected ln_firm_all_ia_d eyear, lcolor(red) msymbol(O) mcolor(red)), ///
	ytitle("{bf:Difference in Log(Est)}") xline(-1, lcolor(black))  xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_firm_all_residual_DD_g3treat, replace)	
	graph export "Figures\ResidualBusiness\Trends_1_2.pdf", as(pdf) replace
	
twoway (connected ln_emp_all_nia_d eyear, lcolor(blue) msymbol(O) mcolor(blue) msymbol(triangle) lpattern(dash)) ///
    (connected ln_emp_all_ia_d eyear, lcolor(red) msymbol(O) mcolor(red)), ///
	ytitle("{bf:Difference in Log(Emp)}") xline(-1, lcolor(black))  xtitle(" ") legend(off)  ///
	xlabel(-4(1)4) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white)) saving(Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat, replace)	
	graph export "Figures\ResidualBusiness\Trends_2_2.pdf", as(pdf) replace
	
	
	grc1leg Figures\ResidualBusiness\ln_firm_all_residual_DD_g3treat.gph  Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat.gph, col(1) ysize(17) xsize(11) legendfrom(Figures\ResidualBusiness\ln_emp_all_residual_DD_g3treat.gph) saving(Figures\ResidualBusiness\combined2, replace)
	graph display, ysize(17) xsize(11) scheme(s1mono) 
    graph export "Figures\ResidualBusiness\Trends_part2.pdf", as(pdf) replace

cd "C:\Research\Tornadoes\0515Miles_CountyM\Figures\ResidualBusiness\"	
	gr combine combined1.gph combined2.gph, ysize(17) xsize(20) scheme(s2mono) plotregion(fcolor(white)) graphregion(fcolor(white))
	graph display, ysize(17) xsize(20) scheme(s2mono) 
	graph export "Appendix_Figure7.pdf", as(pdf) replace
	
